package com.ruoyi.system.service.impl.video;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.VsUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.video.VsDramasVideo;
import com.ruoyi.system.domain.video.VsDramasVideoEpisodes;
import com.ruoyi.system.domain.video.VsDramasVideoLog;
import com.ruoyi.system.mapper.video.VsDramasVideoEpisodesMapper;
import com.ruoyi.system.mapper.video.VsDramasVideoLogMapper;
import com.ruoyi.system.mapper.video.VsDramasVideoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.video.VsDramasVideoFavoriteMapper;
import com.ruoyi.system.domain.video.VsDramasVideoFavorite;
import com.ruoyi.system.service.video.IVsDramasVideoFavoriteService;

/**
 * 收藏点赞Service业务层处理
 * 
 * @author zzx
 * @date 2024-04-30
 */
@Service
public class VsDramasVideoFavoriteServiceImpl implements IVsDramasVideoFavoriteService 
{
    @Autowired
    private VsDramasVideoFavoriteMapper vsDramasVideoFavoriteMapper;

    @Autowired
    private VsDramasVideoMapper vsDramasVideoMapper;

    @Autowired
    private VsDramasVideoLogMapper vsDramasVideoLogMapper;

    @Autowired
    private VsDramasVideoEpisodesMapper vsDramasVideoEpisodesMapper;

    /**
     * 查询收藏点赞
     * 
     * @param id 收藏点赞主键
     * @return 收藏点赞
     */
    @Override
    public VsDramasVideoFavorite selectVsDramasVideoFavoriteById(Integer id)
    {
        return vsDramasVideoFavoriteMapper.selectVsDramasVideoFavoriteById(id);
    }

    /**
     * 查询收藏点赞列表
     * 
     * @param vsDramasVideoFavorite 收藏点赞
     * @return 收藏点赞
     */
    @Override
    public List<VsDramasVideoFavorite> selectVsDramasVideoFavoriteList(VsDramasVideoFavorite vsDramasVideoFavorite)
    {
        VsUser vsUser = null;
        try {
            vsUser = SecurityUtils.getLoginUser().getVsUser();
            vsDramasVideoFavorite.setUserId(vsUser.getId());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        if (vsUser != null) {
            List<VsDramasVideoFavorite> list = vsDramasVideoFavoriteMapper.selectVsDramasVideoFavoriteList(vsDramasVideoFavorite);
            VsDramasVideoLog log = new VsDramasVideoLog();
            log.setUserId(vsUser.getId());
            for (VsDramasVideoFavorite v : list) {
                log.setVid(v.getVid());
                VsDramasVideo video = vsDramasVideoMapper.selectVsDramasVideoById(Math.toIntExact(v.getVid()));
                v.setVideo(video);
                v.getVideo().setFavorite(true);
                List<VsDramasVideoLog> logList = vsDramasVideoLogMapper.selectVsDramasVideoLogList(log);
                if (logList.size() > 0) {
                    VsDramasVideoEpisodes episodes = vsDramasVideoEpisodesMapper.selectVsDramasVideoEpisodesById(Math.toIntExact(logList.get(0).getEpisodeId()));
                    if (!StringUtils.isNull(episodes)) {
                        v.getVideo().setCurrentEpisodeId(Long.valueOf(episodes.getId()));
                        v.getVideo().setCurrentViewTime(logList.get(0).getViewTime());
                        v.getVideo().setCurrentName(episodes.getName());
                    }
                }
            }
            return list;
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 新增收藏点赞
     * 
     * @param vsDramasVideoFavorite 收藏点赞
     * @return 结果
     */
    @Override
    public int insertVsDramasVideoFavorite(VsDramasVideoFavorite vsDramasVideoFavorite)
    {
        VsUser vsUser = SecurityUtils.getLoginUser().getVsUser();
        if (vsUser != null) {
            vsDramasVideoFavorite.setUserId(vsUser.getId());
        }
        vsDramasVideoFavorite.setCreatetime(System.currentTimeMillis());
        return vsDramasVideoFavoriteMapper.insertVsDramasVideoFavorite(vsDramasVideoFavorite);
    }

    /**
     * 修改收藏点赞
     * 
     * @param vsDramasVideoFavorite 收藏点赞
     * @return 结果
     */
    @Override
    public int updateVsDramasVideoFavorite(VsDramasVideoFavorite vsDramasVideoFavorite)
    {
        return vsDramasVideoFavoriteMapper.updateVsDramasVideoFavorite(vsDramasVideoFavorite);
    }

    /**
     * 批量删除收藏点赞
     * 
     * @param ids 需要删除的收藏点赞主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasVideoFavoriteByIds(Integer[] ids)
    {
        return vsDramasVideoFavoriteMapper.deleteVsDramasVideoFavoriteByIds(ids);
    }

    /**
     * 删除收藏点赞信息
     * 
     * @param id 收藏点赞主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasVideoFavoriteById(Integer id)
    {
        return vsDramasVideoFavoriteMapper.deleteVsDramasVideoFavoriteById(id);
    }

    @Override
    public int cancelVideo(VsDramasVideoFavorite vsDramasVideoFavorite) {
        return vsDramasVideoFavoriteMapper.cancelVideo(vsDramasVideoFavorite);
    }
}
